﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gasco.Business.Services;
using Gasco.DataAccess;

public partial class Controls_loading_index : System.Web.UI.UserControl
{
    private string sortDirection
    {
        get
        {
            if (ViewState["sort_direction"] == null)
                ViewState["sort_direction"] = "descending";

            return ViewState["sort_direction"].ToString();
        }
        set
        {
            ViewState["sort_direction"] = value;
        }
    }

    private string sortExpression {
        get
        {
            if (ViewState["sort_expression"] == null)
                ViewState["sort_expression"] = "create_date";

            return ViewState["sort_expression"].ToString();
        }
        set
        {
            ViewState["sort_expression"] = value;
        }
    }

    private LoadingService loadingService { get; set; }

    public LoadingService LoadingService
    {
        get
        {
            if (loadingService == null)
                loadingService = new LoadingService();

            return loadingService;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            GetLoading(null, 1, true);
            loading li = new loading();
        }
    }

    protected void gvLoading_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (this.sortDirection == "ascending")
            this.sortDirection = "descending";
        else
            this.sortDirection = "ascending";

        this.sortExpression = e.SortExpression;

        GetLoading(tbSearch.Text, gvLoading.PageIndex + 1, false);
    }

    protected void gvLoading_Deleting(object sender, GridViewDeleteEventArgs e)
    {
        LoadingService.DeleteLoading(Convert.ToInt32(gvLoading.DataKeys[e.RowIndex].Value));
        GetLoading(tbSearch.Text, 1, true);
    }

    private void GetLoading(string keyword, int pageNumber, bool setPageCount)
    {
        ViewState["CurrentPage"] = pageNumber;

        if (Request.QueryString["order_number"] != null)
        {
            tbSearch.Text = Request.QueryString["order_number"];
            keyword = Request.QueryString["order_number"];
            tbSearch.Visible = false;
            lbSearch.Visible = false;
            newloading.Visible = false;
            lbtitle.Visible = false;
            gvTitle.Visible = false;

        }

        if (setPageCount)
            gvLoading.VirtualItemCount = LoadingService.CountSearchLoading(keyword);

        gvLoading.DataSource = LoadingService.SearchLoading(keyword, gvLoading.PageSize, pageNumber, this.sortExpression, this.sortDirection);
        gvLoading.DataBind();
    }

    protected void gvLoading_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvLoading.PageIndex = e.NewPageIndex;
        GetLoading(tbSearch.Text, e.NewPageIndex + 1, false);
    }

    protected void lbSearch_Click(object sender, EventArgs e)
    {
        GetLoading(tbSearch.Text, 1, true);
    }
}
